package com.coollang.smartbra.utils;

import com.coollang.smartbra.constant.Constants;
import com.coollang.smartbra.views.CloseUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import java.io.File;
import java.io.FileOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by han on 2016/10/27.
 *
 * @author han
 * @date 2016/10/27
 * @Modifier ${MODIFIER}
 * @ModifiedTime ${MODIFIEDDATE}
 * @ModifyContent${MODIFYCONTENT}
 * @since ${VERSIONNBAME}
 * <p>
 * String 工具类
 */

public class StringUtils {

    public static char[] getChar(String str) {
        return str.toCharArray();
    }


    /**
     * 检验手机号码格式是否正确
     *
     * @param phone
     * @return
     */
    public static boolean matchPhone(String phone) {
        String regExp = "^1[3|4|5|7|8][0-9]{9}$";

        Pattern p = Pattern.compile(regExp);

        Matcher m = p.matcher(phone);

        return m.find();//boolean
    }

    public static String stringTxt(byte[] str, String filename) {
        File file1 = new File(Constants.UPDATE_FILE + filename);
        LogUtils.i("UpdateDevice", "fileName == " + filename);
        LogUtils.i("UpdateDevice", "Constants.UPDATE_FILE == " + Constants.UPDATE_FILE);
        file1.mkdirs();
        if (file1.exists()) {
            file1.delete();
        }
        FileOutputStream fos = null;
        try {
            //建立输出字节流
            fos = new FileOutputStream(file1);
            //用FileOutputStream 的write方法写入字节数组
            fos.write(str);
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            CloseUtils.closeQuiety(fos);
        }
        return file1.getAbsolutePath();
    }

    /* 安全解析json防止解析错误造成崩溃 */
    public static Object parseJSonSafe(String json, Object classT) {
        Gson gson = new Gson();
        try {
            return gson.fromJson(json, classT.getClass());
        } catch (JsonSyntaxException e) {
            return null;
        }
    }

    /* 确保字符串转数字不会出现崩溃 */
    public static float parseStringToFloatSafe(String string) {
        if (string != null && !"".equalsIgnoreCase(string.trim())) {// 非空非null
            float a = 0;
            try {
                a = Float.parseFloat(string);
            } catch (NumberFormatException e) {

            }
            return a;
        } else {
            return 0;
        }
    }

    /* 转化成整数 */
    public static int parseStringToIntegerSafe(String string) {
        if (string != null && !"".equalsIgnoreCase(string.trim())) {// 非空非null
            int a = 0;
            try {
                a = Integer.parseInt(string);
            } catch (NumberFormatException e) {

            }
            return a;
        } else {
            return 0;
        }

    }

    /* 转化成long */
    public static long parseStringToLongSafe(String string) {
        if (string != null && !"".equalsIgnoreCase(string.trim())) {// 非空非null
            long a = 0;
            try {
                a = Long.parseLong(string);
            } catch (NumberFormatException e) {

            }
            return a;
        } else {
            return 0;
        }
    }

    public static String getSexString(String string) {
        String result = "";
        if ("1".equals(string)) {
            result = "男";
        } else if ("2".equals(string)) {
            result = "女";
        }
        return result;
    }

    /**
     * s＝s.substring(int begin);截取掉s从首字母起长度为begin的字符串，将剩余字符串赋值给s；
     * s＝s.substring(int begin，int end);截取s中从begin开始至end结束时的字符串，并将其赋值给s;
     *
     * @param string
     * @return
     */
    public static String getOutOfFirstOneString(String string) {
        String result = string.substring(0, string.length());
        return result;
    }
}
